<?php
if (!defined( "IN_UCHOME" ) ) exit( "Access Denied" );
require('class.action.php');
class get extends action
{
	var $page = 0;
	var $limit = 10;
	var $offset = 0;
	var $url = '';
	var $param = '';
	var $order = '';

	function get()
	{
		$temp = array();
		foreach($_GET as $key=>$val){
			if($key == 'page') continue;
			$temp[] = $key . '=' . $val;
		}
		if(count($temp) > 0){
			$this->url = 'action.php?' . implode('&', $temp);
		}else{
			$this->url = 'action.php';
		}
	}

	function set_limit($limit)
	{
		$this->limit = $limit;
	}

	function get_page()
	{
		$this->page = (isset($_GET['page']) && intval($_GET['page']) > 0 ) ? intval($_GET['page']) : 1;
		$this->offset = ($this->page - 1 ) * $this->limit;
	}

	function format_data(&$data)
	{
		global $uid;
		$temp = array();
		foreach($data as $key=>$val){
			if($val['privite'] == 2 && $val['uid'] != $uid) continue;
			$val['form'] = $this->format_form($val['id'], $val, $this->get_event_status($val['id'], $uid));
			realname_set($val['uid'], $val['username']);
			$time = sgmdate('Y-n-d', $val['start_time']);
			if(!isset($temp[$time])){
				$temp[$time]['time'] = $time;
				$temp[$time]['xinqi'] = $this->format_xinqi(sgmdate('D', $val['start_time']));
				$temp[$time]['data'] = array();
			}
			$val['start_time'] = sgmdate('Y-n-d H:i', $val['start_time']);
			$val['end_time'] = sgmdate('Y-n-d H:i', $val['end_time']);
			$val['dateline'] = sgmdate('Y-n-d H:i', $val['dateline'], 1);
			$temp[$time]['data'][] = $val;
		}
		return $temp;
	}

	function get_index($do)
	{
		global $_SCONFIG;
		$d = $_SERVER['REQUEST_TIME'];
		$w = date("w",$d);
		/*为符合国人习惯，按周一为一个星期第一天算*/
		$w_s = mktime(0,0,0,date("m", $d),date("d", $d) - $w + 1, date("Y", $d)) - $_SCONFIG['timeoffset'] * 3600;
		$w_e = $w_s  +  3600 * 24 * 7;
		if($do == 'last'){
			$this->param = " start_time < {$w_s} ";
		}elseif($do == 'week'){
			$day = intval($_GET['day']);
			if($day < 0 || $day > 6) $day = 0 ;
			$t_s = mktime(0,0,0,date("m", $d),date("d", $d), date("Y", $d)) + ($day - 2) * 3600*24  - $_SCONFIG['timeoffset'] * 3600;
			$t_e = $t_s + 3600 * 24;
			$this->param = " start_time >= {$t_s} and start_time < {$t_e} ";
		}elseif($do == 'next'){
			$this->param = " start_time > {$w_e} ";
		}
		return $this->get_data(' id desc ');
	}

	function get_today()
	{
		global $_SCONFIG;
		$start = mktime(0,0,0, date('n', $_SERVER['REQUEST_TIME']), date('d', $_SERVER['REQUEST_TIME']), date('Y', $_SERVER['REQUEST_TIME'])) - $_SCONFIG['timeoffset'] * 3600;
		$end = $start + 3600*24;
		$this->param = " start_time >= {$start} and start_time < {$end} ";
		return $this->get_data(' id desc ');
	}

	function get_city($p, $c)
	{
		$this->param = " p = '{$p}' and c = '{$c}' ";
		return $this->get_data(' id desc ');
	}

	function get_hot()
	{
		return $this->get_data(' join_num desc , interest_num desc');
	}

	function get_ing()
	{
		global $_SCONFIG;
		$this->param = " start_time <= {$_SERVER['REQUEST_TIME']} and end_time >= {$_SERVER['REQUEST_TIME']} ";
		return $this->get_data(' id desc ');
	}

	function get_finished()
	{
		global $_SCONFIG;
		$this->param = " end_time <= {$_SERVER['REQUEST_TIME']} ";
		return $this->get_data(' id desc ');
	}

	function get_my_index()
	{
		global $uid;
		$this->param = " uid = {$uid} ";
		return $this->get_data(' id desc ');
	}

	function _my_common($type)
	{
		global $uid;
		$total = $this->query_one("select count(*) as num from ".tname("app_action_join"). " where uid = '{$uid}' and type = '{$type}' ");
		if($total['num'] > 0){
			$data = $this->query_all("select a.* from ".tname("app_action_join"). " as j left join ".tname("app_action"). " as a on j.id = a.id  where j.uid = '{$uid}' and j.type = '{$type}' order by j.id desc  limit {$this->offset}, {$this->limit} ");
			return array('total'=>$total['num'], 'data'=>$data, 'html'=>multi($total['num'], $this->limit, $this->page, $this->url));
		}else{
			return array('total'=>$total['num'], 'data'=>array(), 'html'=>'');
		}
	}

	function get_my_join()
	{
		return $this->_my_common('join');
	}

	function get_my_interest()
	{
		return $this->_my_common('interest');
	}

	function get_data($order)
	{
		if(!empty($this->param) ){
			$ext = ' where ' . $this->param;
			$this->param = '';
		}else{
			$ext = '';
		}
		$total = $this->query_one("select count(*) as num from ".tname("app_action"). $ext);
		if($total['num'] > 0){
			if($order) $ext .= " order by $order  "; 
			$ext .= " limit {$this->offset}, {$this->limit}";
			$data = $this->query_all("select id, uid, username, subject, type, type_company, p, c, area, eventtype, start_time, end_time, join_num, interest_num, dateline, picture, privite from ".tname("app_action"). $ext);
			return array('total'=>$total['num'], 'data'=>$data, 'html'=>multi($total['num'], $this->limit, $this->page, $this->url));
		}else{
			return array('total'=>$total['num'], 'data'=>array(), 'html'=>'');
		}
	}
}
?>